#include "myhead.h"

pD_music List_Init(const char *dirname)
{
    //创建链表
    head = List_Create();

    //检索目录
    count = FindDir(dirname , head);

    return head;
}


pD_music List_Create()
{
    //申请头节点空间
    pD_music head = malloc(sizeof(D_music));
	if(head == NULL)
	{
		perror("malloc failed!");
		return NULL;
	}

    //头节点赋值
	bzero(head->name,20);
    head->num = 0;
	head->prev = head;
	head->next = head;
    
    //返回头节点
    return head;
}

bool List_Insert_End(pD_music head, char *pic_name , int num)
{
    //新节点申请堆空间
    pD_music newnode = (pD_music)malloc(sizeof(D_music));
    if (NULL == newnode)
    {
        perror("malloc newnode failed");
        return false;
    }
    //对新节点进行赋值
    strcpy(newnode->name, pic_name); //把传入的字符串拷贝到新的结点
    newnode->num = num;//把传入的图片编号拷贝到新的结点
    newnode->prev = newnode;
    newnode->next = newnode;
    //尾插法插入节点
    //处理前继指针
    newnode->prev = head->prev;
    head->prev = newnode;
    //处理后继指针
    newnode->prev->next = newnode;
    newnode->next = head;
    return true;
}

int FindDir( const char *dirname , pD_music music_list )
{
    // 打开目录文件
    DIR * dirp = opendir(dirname);
    if (NULL == dirp)
    {
        perror("opendir failed!");
        return -1;
    }

    while (1)
    {
        // 读取目录项目
        struct dirent * info = readdir( dirp );
        if (info == NULL)
        {
            return count ;
        }
        
        char NewPath [257] = {0};
        snprintf( NewPath , 257 ,"%s/%s" , dirname , info->d_name );

        // 判断是否为目录文件
        if ( info->d_type == 4 &&
             strcmp(info->d_name , ".") &&
             strcmp(info->d_name , "..")  )
        {
            // 打开该目录继续（递归）深度检索
            FindDir( NewPath , music_list );
        }

        // 判断是否为普通文件
        if (info->d_type == 8)
        {
            //判断是否为 “.” 或 “..” 文件
            if (strcmp(".", info->d_name) == 0 || strcmp("..", info->d_name) == 0)
            continue;

            printf("当前的count:%d\n",count+1);
            //检索目录下的.mp3文件
            if(strncmp(".mp3", info->d_name + strlen(info->d_name) - 4,4) == 0)
            {
                List_Insert_End(music_list, NewPath, count+1);
                // 计数值++
                count++ ;
            }
            //检索目录下的.avi文件
            if(strncmp(".avi", info->d_name + strlen(info->d_name) - 4,4) == 0)
            {
                List_Insert_End(music_list, NewPath, count+1);
                // 计数值++
                count++ ;
            }
        }
    }

    closedir(dirp);
}

//遍历----链表
void List_DisPlay(pD_music music_head)
{
    //保存头节点
    pD_music p = music_head->next;
    while (p != music_head)
    {
        printf("name:%s\n", p->name);//视频路径与名字
        printf("num:%d\n", p->num);//视频编号
        p = p->next;
    }
}

//销毁----链表2
int List_Destory(pD_music Head)
{
    pD_music p = Head;
    pD_music q = p->next;
    int i = 0;
    while (q != Head)
    {
        p = q;
        free(p);
        i++;
        q = q->next;
    }
    free(Head);
    return i;
}
